import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int testCases = Integer.parseInt(br.readLine());
		while(testCases-- != 0) {
			br.readLine();
			int n = Integer.parseInt(br.readLine().trim());
			long[] l = new long[n];
			long[] aux = new long[n]; 
			for(int i = 0; i < n; i++) {
				l[i] = Long.parseLong(br.readLine().trim());
			}
			
			System.out.println(mergeSort(l, aux, 0, n - 1));
		}
	}
	
	private static long mergeSort(long[] l, long[] aux, int start, int end) {
		if(start > end) {
			return 0;
		}
		if(start == end) {
			aux[start] = l[start];
			return 0;
		}
		int mid = (start + end) / 2;
		long m1 = mergeSort(l, aux, start, mid);
		long m2 = mergeSort(l, aux, mid + 1, end);
		
		long m3 = 0;
		int index = start;
		int i1 = start, j1 = mid + 1;
		while(i1 <= mid && j1 <= end) {
			if(aux[i1] > aux[j1]) {
				m3 += (mid - i1 + 1);
				l[index++] = aux[j1++];
			} else {
				l[index++] = aux[i1++];
			}
		}
		while(i1 <= mid) {
			l[index++] = aux[i1++];
		}
		while(j1 <= end) {
			l[index++] = aux[j1++];
		}
		for(int i = start; i <= end; i++) {
			aux[i] = l[i];
		}
		
		return m1 + m2 + m3;
	}
	

}
